package com.simple.anno.impl;

import com.simple.anno.AnnotationHandler;
import com.simple.handler.AbstractHandler;
import com.simple.manager.AgentManager;
import com.simple.util.ReflectionsUtil;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Set;

public class AnnotationHandlerImpl {

    public static void annotationHandlerImpl() throws InvocationTargetException, InstantiationException, IllegalAccessException {
        Set<Class<?>> classes = ReflectionsUtil.getAnnotationByClass(AnnotationHandler.class);
        for (Class<?> aClass : classes) {
            Constructor<?> constructor = null;
            try {
                constructor = aClass.getDeclaredConstructor();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
                continue;
            }
            AbstractHandler instance = (AbstractHandler) constructor.newInstance();
            instance.init(aClass.getDeclaredAnnotation(AnnotationHandler.class).aClass()[0]);
            AgentManager.getInstance().register(aClass, instance.createObject());
        }
    }
}
